package com.ctshk.rpc.order.bus.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.common.service.mapper.CtsQueryWrapper;
import com.ctshk.common.utils.Assist;
import com.ctshk.rpc.order.bus.dto.BusOrderPageDTO;
import com.ctshk.rpc.order.bus.entity.BusOrder;
import com.ctshk.rpc.order.bus.req.BusOrderPageReq;

/**
 * <p>
 * 巴士订单表 Mapper 接口
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-24
 */
public interface BusOrderMapper extends CtsBaseMapper<BusOrder> {

//    /**
//     * 获取巴士路线列表
//     *
//     * @param req
//     * @return
//     */
//    @Select("<script>" + "SELECT o.id,o.order_number,o.product_type,o.bus_companies_name,o.line_id, \n" +
//            "o.shift_code,o.departure_city_name,o.arrive_city_name,o.boarding_station_name, \n" +
//            "o.alighting_station_name,o.order_amount,o.payment_status,o.scheduled_status, \n" +
//            "o.contact_name,o.source,o.salesman_name,o.gmt_create \n" +
//            "FROM t_bus_order o \n" +
//            "WHERE o.is_deleted = 0 <if test=\"req.orderNumber != null and req.orderNumber != ''\">AND o.order_number = #{req.orderNumber}</if> \n" +
//            "<if test=\"req.productType != null and req.productType != 0\">AND o.product_type = #{req.productType}</if> \n" +
//            "<if test=\"req.busCompaniesName != null and req.busCompaniesName != ''\">AND o.bus_companies_name LIKE CONCAT('%',#{req.busCompaniesName},'%')</if> \n" +
//            "<if test=\"req.shiftCode != null and req.shiftCode != ''\">AND o.shift_code LIKE CONCAT('%',#{req.shiftCode},'%')</if> \n" +
//            "<if test=\"req.departureCityName != null and req.departureCityName != ''\">AND o.departure_city_name LIKE CONCAT('%',#{req.departureCityName},'%')</if> \n" +
//            "<if test=\"req.arriveCityName != null and req.arriveCityName != ''\">AND o.arrive_city_name LIKE CONCAT('%',#{req.arriveCityName},'%')</if> \n" +
//            "<if test=\"req.boardingStationName != null and req.boardingStationName != ''\">AND o.boarding_station_name LIKE CONCAT('%',#{req.boardingStationName},'%')</if> \n" +
//            "<if test=\"req.alightingStationName != null and req.alightingStationName != ''\">AND o.alighting_station_name LIKE CONCAT('%',#{req.alightingStationName},'%')</if> \n" +
//            "<if test=\"req.scheduledStatus != null and req.scheduledStatus != 0\">AND o.scheduled_status = #{req.scheduledStatus}</if> \n" +
//            "<if test=\"req.paymentStatus != null and req.paymentStatus != 0\">AND o.payment_status = #{req.paymentStatus}</if> \n" +
//            "<if test=\"req.beginDate != null and req.endDate != null\">AND DATE_FORMAT(o.gmt_create,'%Y-%m-%d') between #{req.beginDate} and #{req.endDate}</if> \n" +
//            "<if test=\"req.contactName != null and req.contactName != ''\">AND o.contact_name =#{req.contactName}</if> \n" +
//            "<if test=\"req.source != null and req.source != 0\">AND o.source = #{req.source}</if> \n" +
//            "<if test=\"req.salesmanName != null and req.salesmanName != ''\">AND o.salesman_name =#{req.salesmanName}</if> \n" +
//            "order by  o.gmt_create desc " + "</script>")
//    IPage<BusOrderPageDTO> queryList2(@Param("req") BusOrderPageReq req, IPage<BusOrderPageDTO> agentPage);
    
    /**
     * 查询订单列表
     * @return
     */
    default IPage<BusOrderPageDTO> queryList(BusOrderPageReq req, List<Long> permissionUserList) {
    	if (Assist.isBlank(permissionUserList)) {
    		new Page<>();
    	}
    	CtsQueryWrapper<BusOrder> queryWrapper = newCtsWrapper()
    			.ifNotBlankLike("order_number", req.getOrderNumber())
    			.ifNotNullEq("product_type", req.getProductType())
    			.ifNotBlankLike("bus_companies_name", req.getBusCompaniesName())
    			.ifNotBlankLike("shift_code", req.getShiftCode())
    			.ifNotBlankLike("departure_city_name", req.getDepartureCityName())
    			.ifNotBlankLike("arrive_city_name", req.getArriveCityName())
    			.ifNotBlankLike("boarding_station_name", req.getBoardingStationName())
    			.ifNotBlankLike("alighting_station_name", req.getAlightingStationName())
    			.ifNotNullEq("scheduled_status", req.getScheduledStatus())
    			.ifNotNullEq("payment_status", req.getPaymentStatus())
    			.ifNotNullEq("source", req.getSource())
    			.ifNotBlankLike("contact_name", req.getContactName())
    			.ifNotBlankLike("salesman_name", req.getSalesmanName())
    			.ifNotNullGe("gmt_create", req.getBeginDate())
    			.ifNotNullLt("gmt_create", req.getEndDate())
    			.ifNotEmptyIn("salesman_id", permissionUserList)
    			
    			.orderByDesc("gmt_create")
    			;
    	return selectPage(BusOrderPageDTO.class, req, queryWrapper);
    }

 
    @Select({
            "SELECT count(bus_id) FROM t_bus_order ",
            "where is_deleted = 0 ",
            "and bus_id = #{id} ",
            "and (payment_status = 2 or scheduled_status = 1)  ",
            "and  DATEDIFF(NOW(),gmt_create) BETWEEN 0 AND 7  "
    })
    Integer selectSaleCount(@Param("id") Long id);
}
